﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Data;
using PayRoller.Code;
using HySoft.Cash;

namespace PayRoller.Include
{
    public partial class UPaymentDisplay : System.Web.UI.UserControl
    {
        public int OrgID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                this.DisplaySettingsStatus();
                this.BindInfo();
            }
        }

        public void BindInfo()
        {
            if (OrgID > 0)
            {
                this.BindClientInfo();
                this.BindEmployeeInfo();
            }
        }

        private void BindClientInfo()
        {
            using (TorginfoBN obn = new TorginfoBN(this.Page))
            {
                TorginfoDT detail = obn.Get(this.OrgID);
                if (detail != null)
                {
                    this.labClientName.Text = detail.OrgName;
                    this.labOurReference.Text = detail.Sid.ToString();
                }
            }
        }

        private void BindEmployeeInfo()
        {
            using (TempinfoBN obn2 = new TempinfoBN(this.Page))
            {
                //需要根据当前企业ID获取其下的员工列表
                obn2.QueryOrgSid(this.OrgID);
                DataTable table = obn2.GetList();
                this.dlEmployee.DataSource = table;
                this.dlEmployee.DataBind();
            }
        }

        /// <summary>
        /// 本方法设置了 需要显示隐藏的部分配置项；其他的配置项在 DataList的绑定事件中设置。
        /// </summary>
        private void DisplaySettingsStatus()
        {
            ReportSettingsPaymentDT setting = GetReportSettings();
            if (setting != null)
            {
                this.trClientName.Visible = setting.IsDisplayOrgName;
                this.trOurReference.Visible = setting.IsDisplaySystemReference;
                this.dlEmployee.Visible = setting.IsDisplayEmployeeList;
            }
        }

        private ReportSettingsPaymentDT GetReportSettings()
        {
            ReportSettingsBN business = new ReportSettingsBN(this.Page);
            ReportSettingsPaymentDT setting = business.GetReportSettingsPayment(0);
            return setting;
        }

        protected void dlEmployee_ItemCreated(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            ReportSettingsPaymentDT setting = GetReportSettings();
            switch (itemType)
            {
                case ListItemType.Header:
                    e.Item.FindControl("thEmployeeID").Visible = setting.IsDisplayEmployeeID;
                    e.Item.FindControl("thEmployeeName").Visible = setting.IsDisplayEmployeeName;
                    break;
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    e.Item.FindControl("tdEmployeeID").Visible = setting.IsDisplayEmployeeID;
                    e.Item.FindControl("tdEmployeeName").Visible = setting.IsDisplayEmployeeName;
                    e.Item.FindControl("dlPayment").Visible = setting.IsDisplayPaymentList;
                    break;
            }
        }

        protected void dlEmployee_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            ReportSettingsPaymentDT setting = GetReportSettings();
            if (setting.IsDisplayPaymentList == false)
            {
                return;
            }

            if (itemType == ListItemType.AlternatingItem || itemType == ListItemType.Item || itemType == ListItemType.SelectedItem)
            {
                DataList dlPayment = e.Item.FindControl("dlPayment") as DataList;
                DataRowView drv = e.Item.DataItem as DataRowView;
                if (dlPayment != null && drv != null)
                {
                    dlPayment.ItemCreated += new DataListItemEventHandler(dlPayment_ItemCreated);
                    dlPayment.ItemDataBound += new DataListItemEventHandler(dlPayment_ItemDataBound);

                    //获取当前员工的id，接着获取当前员工的payment进行绑定
                    int currentEmployeeID = Convert.ToInt32(drv["Sid"]);
                    TemppaymentBN paymentBN = new TemppaymentBN(this.Page);
                    paymentBN.TableAliasName = "P.";
                    paymentBN.QueryEmpSid(currentEmployeeID);
                    DataTable paymentTable = paymentBN.GetListJoinBank();
                    dlPayment.DataSource = paymentTable;
                    dlPayment.DataBind();
                }
            }
        }

        void dlPayment_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            DataRowView drv = e.Item.DataItem as DataRowView;
            if (drv != null)
            {
                switch (itemType)
                {
                    case ListItemType.Item:
                    case ListItemType.AlternatingItem:
                    case ListItemType.SelectedItem:
                        Label labBSB = e.Item.FindControl("labBSB") as Label;
                        if (labBSB != null)
                        {
                            labBSB.Text = drv["Bsb"].ToString();
                        }

                        Label labAccountNum = e.Item.FindControl("labAccountNum") as Label;
                        if (labAccountNum != null)
                        {
                            labAccountNum.Text = drv["AccountNum"].ToString();
                        }

                        Label labBankName = e.Item.FindControl("labBankName") as Label;
                        if (labBankName != null)
                        {
                            labBankName.Text = drv["BankName"].ToString();
                        }


                        Label labPayReference = e.Item.FindControl("labPayReference") as Label;
                        if (labPayReference != null)
                        {
                            labPayReference.Text = drv["PayReference"].ToString();
                        }


                        Label labPaymentDate = e.Item.FindControl("labPaymentDate") as Label;
                        if (labPaymentDate != null)
                        {
                            labPaymentDate.Text = Convert.ToDateTime( drv["PaymentDate"]).ToString(AppSettings.DateFormatAU);
                        }


                        Label labPaymentAmount = e.Item.FindControl("labPaymentAmount") as Label;
                        if (labPaymentAmount != null)
                        {
                            labPaymentAmount.Text = drv["PaymentAmount"].ToString();
                        }
                        break;
                }
            }
        }

        void dlPayment_ItemCreated(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            ReportSettingsPaymentDT setting = GetReportSettings();
            switch (itemType)
            {
                case ListItemType.Header:
                    e.Item.FindControl("thBSB").Visible = setting.IsDisplayBSB;
                    e.Item.FindControl("thAC").Visible = setting.IsDisplayAC;
                    e.Item.FindControl("thBankName").Visible = setting.IsDisplayBankName;
                    e.Item.FindControl("thReference").Visible = setting.IsDisplayReference;
                    e.Item.FindControl("thPaymentDate").Visible = setting.IsDisplayPaymentDate;
                    e.Item.FindControl("thAmount").Visible = setting.IsDisplayAmount;
                    break;
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    e.Item.FindControl("tdBSB").Visible = setting.IsDisplayBSB;
                    e.Item.FindControl("tdAC").Visible = setting.IsDisplayAC;
                    e.Item.FindControl("tdBankName").Visible = setting.IsDisplayBankName;
                    e.Item.FindControl("tdReference").Visible = setting.IsDisplayReference;
                    e.Item.FindControl("tdPaymentDate").Visible = setting.IsDisplayPaymentDate;
                    e.Item.FindControl("tdAmount").Visible = setting.IsDisplayAmount;
                    break;
            }
        }
    }
}